/**
 * This file is part of ossecdb-extjs.
 *
 * Copyright (C) 2011 Brendan Johnston
 *
 * Contact: brendan@johnston.net.au
 *
 * Project: http://code.google.com/p/ossecdb-extjs/
 *
 * License: http://www.gnu.org/licenses/gpl-3.0.html
 */

Ext.define('OssecWeb.controller.Main', {
   
	extend: 'Ext.app.Controller',

	stores: [
		'Alerts', 
		'Locations'
	],

	models: [
		'Alert', 
		'Location'
	],

	views: [
		'alert.Grid', 
		'location.Tree'
	],

	refs: [
		{ref: 'alertGrid', selector: 'alertgrid'},
		{ref: 'locationTree', selector: 'locationtree'}
	],

	// ISSUE: Is it safe to use node.data.attribute instead of node.get('attribute')?
	getLocationNode: function (node) {
		do {
			if (node.data.type == 'location') {
				return node;
			}
		} while (node = node.parentNode);
	},

	// ISSUE: Is it safe to use node.data.attribute instead of node.get('attribute')?
	getLocationFilter: function (node) {

		var filter = {host: node.data.text};

		if (node.data.checked) {

			return filter;

		} else {

			filter.user = [];
			filter.path = [];

			node.cascadeBy(function (cnode) {
				if (cnode.data.leaf && cnode.data.checked) {
					filter[cnode.data.type].push(cnode.data.text);
				}
			});

			if (filter.user.length == 0) {
				delete filter.user;
			}

			if (filter.path.length == 0) {
				delete filter.path;
			}

			if (filter.user || filter.path) {
				return filter;
			}
		}
	},
	
	locationFilters: [],

    init: function() {
		this.control({
			'locationtree': {
				// ISSUE: Is it safe to use node.data.attribute instead of node.get('attribute')?
				checkchange: function (node, checked) {

					var lnode = this.getLocationNode(node);
					
					if (lnode) {
						this.locationFilters[lnode.data.text] = this.getLocationFilter(lnode);
					}

					var rnode = this.getLocationsStore().getRootNode();
					
					delete this.locationFilterJson;

					if (rnode.data.checked == false) {

						var locationFilter = [];
						
						rnode.eachChild(function (lnode) {

							if (lnode.data.checked) {
								if (this.locationFilters[lnode.data.text] == null) {
									this.locationFilters[lnode.data.text] = this.getLocationFilter(lnode);
								}
							}

							if (this.locationFilters[lnode.data.text]) {
								locationFilter.push(this.locationFilters[lnode.data.text]);
							}

						}, this);

						this.locationFilterJson = Ext.JSON.encode(locationFilter);
					}

					this.getAlertsStore().loadPage(1);
				}
			}
		});
	},

	onLaunch: function () {
		this.getAlertsStore().addListener('beforeload', this.prepareAlertRequest, this);
	},

	prepareAlertRequest: function (store, operation, options) {
		
		var params = {};

		if (this.locationFilterJson) {
			params.loc_filter = this.locationFilterJson;
		}

		options.params = options.params || {};
		Ext.apply(options.params, params);

		// ISSUE: Due to bug (EXTJSIV-2532), best to apply to operation object (for time being)
		// FIXED: Fixed in 4.0.4 (which isn't out yet).
		operation.params = operation.params || {};
		Ext.apply(operation.params, params);
	}

});